//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by mafeng on 16/7/26.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#define kReuse @"cellReuse"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *nameArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
@property (nonatomic, strong) NSMutableArray<NSString *> *selectNameArray;
//@property (nonatomic, strong) NSMutableArray<NSString *> *select
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.selectIndexArray = [NSMutableArray array];
    NSArray *array= @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    
    self.nameArray = [NSMutableArray arrayWithArray:array];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReuse];
    NSLog(@"%@", _nameArray);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // tableview 编辑
    // 1. 设置右BarbuttonItem 为编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.editButtonItem.action = @selector(edit);
}
- (void)edit {
    [self performSelector:NSSelectorFromString(@"_toggleEditing")];
}
#pragma mark -- delegate 的方法
// 移动代码的快捷键 command + option + [
// 3. 返回cell的编辑状态或样式, 默认为delete
// 只有这一个方法是Delegate的方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 返回两个是多选
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在全选状态下, 数组中添加对应选中的下标
        NSString *name = _nameArray[indexPath.row];
        [_selectNameArray addObject:name];
        [_selectIndexArray addObject:indexPath];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
         // 在全选状态下, 数组中移除对应选中的下标
        NSString *name = _nameArray[indexPath.row];
        [_selectNameArray removeObject:name];
        [_selectIndexArray removeObject:indexPath];
    }
}
#pragma Mark -- datasourse 方法
// 能否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}
// 移动完成, 更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
// 1. 让tableView进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        [_nameArray removeObjectsInArray:_selectNameArray];
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationLeft];
        [_selectIndexArray removeAllObjects];
        [_selectNameArray removeAllObjects];
        NSLog(@"完成");
    }
}
// 2. 控制tableView 能否被编辑, no 不能编辑, yes能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 5. 提交编辑状态, 对应按钮的点击方法
// 滑动删除, 只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
#warning 如果对tableView 进行添加/删除, 先修改数据源, 再去进行对应的添加删除
        [self.nameArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"插入");
        [_nameArray insertObject:@"王璜" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
